/* Team Directories
 *
 * You are free to do whatever you want with this code. Frankly we don't give a damn.
 * 
 *  
 * Copyright GNU General Public License v3
 */

package StudyBuddy;

import java.io.*;
import java.net.*;
import javax.swing.*;
/**
 *
 * @author  Gabriel
 */
public class BuddyLoader extends javax.swing.JFrame implements Runnable{
    

    PluginCollection collection;
    
    /** Creates new form BuddyLoader */
    public BuddyLoader() {
        collection = new PluginCollection();
        initComponents();
        
            try{  
                collection.loadListFromFile(); 
                
                //collection.loadSavedGameFromFile();
            }

            catch (MalformedURLException e) {
                    JOptionPane.showMessageDialog(null,"The URL is Malformed. But since it's generated by toURL you should never see this Error");

            } catch (ClassNotFoundException e) {
                    JOptionPane.showMessageDialog(null,"ClassNotFoundException \nThis means quite simply that the \"class\" was not found.\nThe file you're trying to open is NOT A CLASS!");

            }catch (IOException e) {
                    JOptionPane.showMessageDialog(null,"General IO Exception - Figure the problem out yourself");

            } catch (InstantiationException e) {
                    JOptionPane.showMessageDialog(null,"Instantiation Exception - Class may not be a class");

            }catch (IllegalAccessException e) {
                    JOptionPane.showMessageDialog(null,"Illegal Access Exception - Class may be in use");

            }catch (NoClassDefFoundError e){
                    JOptionPane.showMessageDialog(null,"No Class Def Found \nEither dependent classes are missing or your buddy is not in a folder of name equal to the package");
            }
            catch (DoubleBuddyException e){
                    JOptionPane.showMessageDialog(null, "Double Buddy Buddy Exception \nThe Buddy is already Loaded");
            }
            catch (Exception e){
                    JOptionPane.showMessageDialog(null,"Strange Exception ... We couldn't figure out what it was!! YOU'RE ON YOUR OWN!!");
            }
            refresh();
        
    }
    
    public void run(){
            
       this.setVisible(true);
        
    }
    
    public void refresh(){
        
        BuddyCollection.removeAllItems();
        for (int i = 0; i<collection.getSize(); i++){ 
            System.out.println(collection.getPluginName(i));
            BuddyCollection.addItem(collection.getPluginName(i));
        }
    
    }
    
    
    public PluginCollection getPluginCollection(){
 //       System.out.println("Collection.toString " + collection.toString());
        return collection;       
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        AddBuddy = new javax.swing.JButton();
        DeleteBuddy = new javax.swing.JButton();
        BuddyCollection = new javax.swing.JComboBox();
        StartBuddy = new javax.swing.JButton();
        Statisics = new javax.swing.JButton();
        Logout = new javax.swing.JButton();
        DisplayHighScores = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Study Buddy");
        setResizable(false);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Buddy", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18)));

        AddBuddy.setFont(new java.awt.Font("Tahoma", 1, 14));
        AddBuddy.setText("Add Buddy");
        AddBuddy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddBuddyActionPerformed(evt);
            }
        });

        DeleteBuddy.setFont(new java.awt.Font("Tahoma", 1, 14));
        DeleteBuddy.setText("Delete Buddy");
        DeleteBuddy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteBuddyActionPerformed(evt);
            }
        });

        BuddyCollection.setFont(new java.awt.Font("Tahoma", 1, 18));

        StartBuddy.setFont(new java.awt.Font("Tahoma", 1, 14));
        StartBuddy.setText("Start Buddy");
        StartBuddy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartBuddyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(StartBuddy, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(AddBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(DeleteBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(BuddyCollection, 0, 819, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(BuddyCollection, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(192, 192, 192)
                        .addComponent(StartBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(AddBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DeleteBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(41, 41, 41))
        );

        Statisics.setFont(new java.awt.Font("Tahoma", 1, 14));
        Statisics.setText("Statisics");
        Statisics.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StatisicsActionPerformed(evt);
            }
        });

        Logout.setFont(new java.awt.Font("Tahoma", 1, 14));
        Logout.setText("Logout");
        Logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogoutActionPerformed(evt);
            }
        });

        DisplayHighScores.setFont(new java.awt.Font("Tahoma", 1, 14));
        DisplayHighScores.setText("High Scores");
        DisplayHighScores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DisplayHighScoresActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(96, 96, 96)
                        .addComponent(Logout, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 351, Short.MAX_VALUE)
                        .addComponent(DisplayHighScores, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addComponent(Statisics, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(69, 69, 69))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(111, 111, 111)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Statisics, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Logout, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DisplayHighScores, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(73, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void StartBuddyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartBuddyActionPerformed
        collection.runPlugin((int)BuddyCollection.getSelectedIndex());
        //DifficultyFrame DF = new DifficultyFrame(this);
        //DF.setVisible(true);
    }//GEN-LAST:event_StartBuddyActionPerformed

    private void AddBuddyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddBuddyActionPerformed
        JFileChooser sc = new JFileChooser();
        int returnValue = sc.showOpenDialog(null);
        try{
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = sc.getSelectedFile();
                collection.addPlugin(selectedFile);
                BuddyCollection.addItem(collection.getPluginName(collection.getSize()-1));
                }
            }
        
        catch (MalformedURLException e) {
                JOptionPane.showMessageDialog(null,"The URL is Malformed. But since it's generated by toURL you should never see this Error");

        } catch (ClassNotFoundException e) {
                JOptionPane.showMessageDialog(null,"ClassNotFoundException \nThis means quite simply that the \"class\" was not found.\nThe file you're trying to open is NOT A CLASS!");

        }catch (IOException e) {
                JOptionPane.showMessageDialog(null,"General IO Exception - Figure the problem out yourself");

        } catch (InstantiationException e) {
                JOptionPane.showMessageDialog(null,"Instantiation Exception - Class may not be a class");

        }catch (IllegalAccessException e) {
                JOptionPane.showMessageDialog(null,"Illegal Access Exception - Class may be in use");

        }catch (DoubleBuddyException e){
                    JOptionPane.showMessageDialog(null, "Double Buddy Buddy Exception \nThe Buddy is already Loaded");
            }
        catch (Exception e){
            JOptionPane.showMessageDialog(null,"Strange Exception ... We couldn't figure out what it was!!");
            
        }
        collection.writeListToFile();
            // refresh();
    }//GEN-LAST:event_AddBuddyActionPerformed

    private void DeleteBuddyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteBuddyActionPerformed
        collection.removePlugin(BuddyCollection.getSelectedIndex());
        BuddyCollection.removeAllItems();
        for (int i = 0; i<collection.getSize(); i++){
            System.out.println(i);
            BuddyCollection.addItem(collection.getPluginName(i));
        }
        collection.writeListToFile();
    }//GEN-LAST:event_DeleteBuddyActionPerformed

    private void StatisicsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StatisicsActionPerformed
        StatisticsGUI newStatsFrame = new StatisticsGUI(this.getPluginCollection());
        newStatsFrame.run();
      //  Scoring score = new Scoring(this.getPluginCollection());
        //score.write();
        //score.DisplayGraph();
       // score.display();
        //score.getUser();
        //score.DisplayGraph();
    }//GEN-LAST:event_StatisicsActionPerformed

    private void LogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogoutActionPerformed
        LoginFrame LF = new LoginFrame();
        //collection.saveGameToFile();
        LF.setVisible(true);
        this.setVisible(false);
        Scoring score = new Scoring(this.getPluginCollection());
        score.checkScore();
        dispose();
    }//GEN-LAST:event_LogoutActionPerformed

    private void DisplayHighScoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DisplayHighScoresActionPerformed
        // TODO add your handling code here:
        
         Scoring score = new Scoring(this.getPluginCollection());
         score.displayHighScore();
    }//GEN-LAST:event_DisplayHighScoresActionPerformed
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddBuddy;
    private javax.swing.JComboBox BuddyCollection;
    private javax.swing.JButton DeleteBuddy;
    private javax.swing.JButton DisplayHighScores;
    private javax.swing.JButton Logout;
    private javax.swing.JButton StartBuddy;
    private javax.swing.JButton Statisics;
    private javax.swing.JPanel jPanel5;
    // End of variables declaration//GEN-END:variables
    
}
